package com.baizhi.lucenceutil;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.tomcat.jni.Directory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;
import java.io.IOException;

/**
 * @Author: renpu
 * @Date: 2019/3/12 19:14
 * @Description:
 */
public class LucenceUtil {

    //获取创建索引库位置对象；
    private static FSDirectory directory;

    //使用lucence的版本；
    private static Version version;

    //分词器
    public static Analyzer analyzer;

    //索引库读取对象；
    private static DirectoryReader reader;


    //放在静态代码块里,类加载的时候仅仅只执行一次；
    static {
        try {
            directory=FSDirectory.open(new File("D://index"));
            version=Version.LUCENE_44;
            analyzer=new IKAnalyzer();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    //获取索引写出对象；
    public static IndexWriter getIndexWriter(){
        IndexWriterConfig indexWriterConfig=new IndexWriterConfig(version,analyzer);
        IndexWriter indexWriter=null;
        try {
            //索引写入对象所必须的参数：索引库的位置,分词器,lucence的使用版本；
             indexWriter=new IndexWriter(directory,indexWriterConfig);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexWriter;
    }


    //获取查询对象；
public static IndexSearcher getIndexSeacher(){
    try {
        reader=DirectoryReader.open(directory);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new IndexSearcher(reader);
}

//提交事务；
    public static void indexCommit(IndexWriter indexWriter){
        try {
            indexWriter.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


//关闭资源
    public static void indexClose(IndexWriter indexWriter){
        try {
            indexWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //回滚资源；
    public static void indexRollerBack(IndexWriter indexWriter){
        try {
            indexWriter.rollback();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //获取Query对象；
    public static Query getQuery(String keyWord){
        //设置包含要查询的对象字段的数据；
        String[] files={"articleId","articleImage","articleName","articleContent","guruId"};
        MultiFieldQueryParser multiFieldQueryParser=new MultiFieldQueryParser(version, files,analyzer);
        Query query=null;
        try {
             query = multiFieldQueryParser.parse(keyWord);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return query;
    }



    public static boolean judgeIndexDB(){
        IndexSearcher indexSearcher = null;
        Query query = new MatchAllDocsQuery();
        TopDocs topDocs = null;
        try {
            indexSearcher =getIndexSeacher();
            topDocs = indexSearcher.search(query, 10000);
        } catch (Exception e) {
            return false;
        }
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        if (scoreDocs.length==0){
            return false;
        }
        return true;
    }
}
